{extend name="platform/base" /}
{block name="main"}
<script>

    //选择顶级
    function VarietyList_1(obj,second,name) {
        var id = $(obj).find("option:selected").val();
        $.ajax({
            type: "post",
            url: __URL(PLATFROMMAIN + "/Goods/ladder"),
            dataType: "json",
            data: {
                "variety_id": id
            },
            success: function (data) {
                console.log(id, data.length);
                if(id==0 || id==''){
                    $(".VarietyList").html('');
                    $(".VarietyList").css('display','none');
                    return false;
                }
                if(second=="#VarietyList_2"){
                    $(".VarietyList").html('');
                    $(".VarietyList").css('display','none');
                }else if(second=="#VarietyList_3"){
                    $("#VarietyList_3").html('');
                    $("#VarietyList_3").css('display','none');
                }
                if ( data.length > 0) {
                    var str = "<option value=''>"+ name +"</option>";
                    for (var i = 0; i < data.length; i++) {
                        str += '<option value="' + data[i].variety_id + '">' + data[i].variety_name + '</option>';
                    }
                    $(second).css('display','');
                    $(second).html(str);
                }else{
                    $(second).css('display','none');
                    $(second).html('');
                }
            }
        });
    }
    //上传方法
    function imgUpload(event) {
        var fileid = $(event).attr("id");
        var data = new Object();
        var id = $(event).next().attr("id");
        var only_type = $(event).attr('only-type');
        if(!only_type){
            data.file_path = UPLOADCOMMON;
        }else{
            data.file_path = UPLOADFILE;
        }
        uploadFile(fileid,data,function(res){

            if(res.code){
                $("#" + id).val(res.data);
                $("#text_" + id).val(res.data);
                $("#preview_"+ id).attr("src",__IMG(res.data));
                showTip(res.message,"success");
            }else{
                showTip(res.message,"error");
            }
        });
    }
</script>
<section class="panel panel-default">
    <header class="panel-heading font-bold"> {if condition="$data"} 修改{else/}添加{/if}</header>
    <div class="panel-body">
        <form class="form-horizontal" id="user_form" name="form">
            <div class="form-group">

                <label class="col-sm-2 control-label"><span class="color-red">*</span>名称</label>
                <div class="col-md-2">
                    <input type="text" class="form-control" onkeydown="error_hide()" id="variety_name"
                           name="variety_name" value='{if condition="$data"} {$data["variety_name"]}{/if}' />
                </div>
                <p class="error">请输入名称</p>
            </div>
            <div class="line line-dashed line-lg pull-in"></div>
            <input type="hidden" id="variety_id" value='{if condition="$variety_id" }{$variety_id}{/if}'>

            {if condition="$data" }
            <div class="form-group" id="VarietyList_error" data-boole="false">
                <label class="col-sm-2 control-label"><span class="color-red">*</span>所属级别</label>
                <div class="col-sm-2">
                    <select id="VarietyList_0" name="type" class="form-control m-b"  onchange="VarietyList_1(this,'#VarietyList_2','请选择二级');">
                        <option value="">请选择一级</option>
                        <option value="0">创建顶级</option>
                        {foreach name="varietyData1.value" item="vo"}
                        <option value="{$vo.variety_id}" {if condition="$varietyData1.key==$vo.variety_id" } selected {/if}>{$vo.variety_name}</option>
                        {/foreach}
                    </select>
                </div>

                <div class="col-sm-2" >
                    <select id="VarietyList_2" name="type" class="form-control m-b VarietyList" style='display: {if condition="$varietyData2.key" } block {else/} none {/if}'  onchange="VarietyList_1(this,'#VarietyList_3','请选择三级');">
                        {foreach name="varietyData2.value" item="vo2"}
                        <option value="">请选择二级</option>
                        <option value="{$vo2.variety_id}" {if condition="$varietyData2.key==$vo2.variety_id" } selected {/if}>{$vo2.variety_name}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="col-sm-2" >
                    <select id="VarietyList_3" name="type" class="form-control m-b VarietyList" style='display: {if condition="$varietyData3.key" } block {else/} none {/if}'   >
                        {foreach name="varietyData3.value" item="vo2"}
                        <option value="">请选择三级</option>
                        <option value="{$vo2.variety_id}" {if condition="$varietyData3.key==$vo2.variety_id" } selected {/if}>{$vo2.variety_name}</option>

                        {/foreach}

                    </select>
                </div>
                <p class="error">请选择所属级别</p>
            </div>
            {else/}
                <div class="form-group" id="VarietyList_error" data-boole="false">
                    <label class="col-sm-2 control-label"><span class="color-red">*</span>所属级别</label>
                    <div class="col-sm-2">
                        <select id="VarietyList_0" name="type" class="form-control m-b"  onchange="VarietyList_1(this,'#VarietyList_2','请选择二级');">
                            <option value="">请选择一级</option>
                            <option value="0">创建顶级</option>
                            {foreach name="VarietyList" item="vo"}
                            <option value="{$vo.variety_id}">{$vo.variety_name}</option>
                            {/foreach}
                        </select>
                    </div>

                    <div class="col-sm-2" >
                        <select id="VarietyList_2" name="type" class="form-control m-b VarietyList" style="display: none;"  onchange="VarietyList_1(this,'#VarietyList_3','请选择三级');">
                            <option value="">请选择二级</option>
                        </select>
                    </div>
                    <div class="col-sm-2" >
                        <select id="VarietyList_3" name="type" class="form-control m-b VarietyList" style="display: none;"  >
                            <option value="">请选择三级</option>
                        </select>
                    </div>
                    <p class="error">请选择所属级别</p>
                </div>
            {/if}
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
                    <label class="col-sm-2 control-label">状态</label>
                    <div class="col-md-2">
                        <div class="col-md-3">
                            <p>是否开启</p>
                            <label class="switch"><input id="type_s" type="checkbox"  {if condition="$data" }{if condition="$data.type==1" } checked="checked"{/if}{/if} value="1" ><span></span>
                            </label>
                        </div>
                    </div>
                <div class="col-md-5">
                    <div class="col-md-3">
                        <p>是否首页显示</p>
                        <label class="switch"><input id="place" type="checkbox"  {if condition="$data" }{if condition="$data.place==1" } checked="checked"{/if}{/if}  value="1"><span></span>
                        </label>
                    </div>
                </div>
            </div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
                <label class="col-sm-2 control-label">排序</label>
                <div class="col-md-4">
                    <input type="number" class="form-control" id="sort" min="0" value="0">
                </div>
            </div>
            <div class="line line-dashed line-lg pull-in"></div>

            <div class="form-group">
                <label class="col-sm-2 control-label">图片</label>
                <div class="col-sm-2">
                    <p class="imglogo-box"><img id="preview_logo" src='{if condition="$data" }{if condition="$data.image" }{$data.image}{/if}{/if}' class="imglogo"/></p>
                    <a href="javascript:void(0);" style="display:block;position:relative;z-index:1;">
	              	<span style="width: 97px;height: 34px;position: absolute;left: 0;top: 0;z-index: 2;cursor: pointer;">
	            	<input hidefocus="true" size="1" class="input-file" name="file_upload" id="uploadimg" nc_type="change_store_label" type="file" style="width: 97px;height: 34px;padding: 0;margin: 0;border: none 0;opacity: 0;filter: alpha(opacity = 0);cursor: pointer;"	onchange="imgUpload(this);">
					<input type="hidden" id="logo" value='{if condition="$data" }{if condition="$data.image" }{$data.image}{/if}{/if}' />
					</span>
                        <a class="btn btn-default"><i class="fa fa-cloud-upload text"></i><span class="text">上传图片</span></a>
                    </a>
                </div>
            </div>
            <div class="line line-dashed line-lg pull-in"></div>



            <div class="line line-dashed line-lg pull-in"></div>


            <div class="form-group">
                <label class="col-sm-2 control-label">图片链接</label>
                <div class="col-md-4">
                    <input type="text" class="form-control" id="url" value='{if condition="$data" }{if condition="$data.url" }{$data.url}{/if}{/if}'>
                </div>
            </div>
            <div class="line line-dashed line-lg pull-in"></div>


            <div class="form-group">
                <label class="col-sm-2 control-label">描述</label>
                <div class="col-sm-4">
                    <div id="editor" class="form-control" style="overflow:scroll;height:150px;max-height:150px">{if
                        condition="$data"}{$data.desc}{/if}
                    </div>
                </div>
            </div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
                <div class="col-sm-4 col-sm-offset-2">
                    <a href="javascript:void(0);" onclick="addUserAjax()" class="btn btn-primary">保存</a>
                </div>
            </div>
        </form>
    </div>
</section>
<script>

    // 防止重复提交 flag为true则表示正在提交 flag为false则表示不是正在提交
    var flag = false;

    /**
     *添加用户
     **/
    function addUserAjax() {
        var variety_id = $("#variety_id").val();
        var variety_name = $("#variety_name").val();
        var type = $("#type_s:checked").val()?1:2;
        var place = $("#place:checked").val()?1:0;
        var sort = $("#sort").val();
        var desc = $("#editor").html();
        var logo = $("#logo").val();
        var url = $("#url").val();
        if($('#VarietyList_3').val()){
           var variety_pid = $('#VarietyList_3').val();
        }else if($('#VarietyList_2').val()){
            var variety_pid = $('#VarietyList_2').val();

        }else if($('#VarietyList_0').val()){
            var variety_pid =  $('#VarietyList_0').val();
        }
        if (verify() && !flag) {
            flag = true;
            $.ajax({
                type: "post",
                url: "{:__URL('PLATFORM_MAIN/Goods/addGoodsVariety')}",
                data: {
                    'variety_id': variety_id,
                    'variety_name': variety_name,
                    'variety_pid': variety_pid,
                    'type': type,
                    'desc': desc,
                    'image': logo,
                    'url': url,
                    'place': place,
                    'sort': sort
                },
                async: true,
                success: function (data) {
                    flag = false;
                    if (data > 0) {
                        showMessage('success', '成功', "{:__URL('PLATFORM_MAIN/Goods/GoodsVariety')}");
                    } else {
                        showMessage('error', '失败');
                    }
                }
            });
        }

    }

    // 添加或修改的验证
    function verify() {

        var variety_name = $("#variety_name").val();
        var desc = $("#editor").html();
        var VarietyList_0 = $("#VarietyList_0").val();

        var reg_name = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？_]");
        if ($.trim(variety_name) == '') {
            $("#variety_name").focus().parent().next().html('请输入用户名').show();
            console.log('a');
            return false;
        } else if (reg_name.test(variety_name)) {
            $("#variety_name").focus().parent().next().html('用户名不能包含特殊字符').show();
            console.log('b');

            return false;
        }


        if (VarietyList_0 == '') {
            console.log('c');

            $("#VarietyList_0").focus().parent().next().show();
            return false;
        }

        return true;
    }
</script>
{/block}